﻿
Imports System.Runtime.CompilerServices
Imports osi.root.delegates
Imports osi.root.utils

Public Module _event_comb_extensions
    <Extension()> Public Function success_finished(ByVal this As event_comb) As Boolean
        Return Not this Is Nothing AndAlso this.end() AndAlso this.end_result()
    End Function

    Friend Function create(ByVal precondition As _do(Of Boolean),
                           ByVal ctor As _do(Of event_comb)) As event_comb
        assert(Not precondition Is Nothing)
        assert(Not ctor Is Nothing)
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  If precondition() Then
                                      ec = ctor()
                                      Return waitfor(ec) AndAlso
                                             goto_next()
                                  Else
                                      Return False
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         goto_end()
                              End Function)
    End Function
End Module
